package util;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtil {

    private static final ComboPooledDataSource dataSource;

    static {
        //dataSource资源只能初始化一次
        dataSource = new ComboPooledDataSource("testc3p0");
    }

    public static Connection getConnection() {
        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            String url = "jdbc:mysql://154.91.179.153:3306/db_003";
//            Connection conn = DriverManager.getConnection(url, "user003", "222019#web");
            Connection conn = dataSource.getConnection();
            return conn;
        } catch (/*ClassNotFoundException | */SQLException cnfe) {
            cnfe.printStackTrace();
            return null;
        }
    }

    public static void freeConnection(Connection conn) {
        try {
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
